<template>
  <div class="tab-bar-item" @click="itemClick">
    <div class="item-icon" v-if="!isActive"><slot name="item-icon"></slot></div>
    <div class="item-icon-active" v-else>
      <slot name="item-icon-active"></slot>
    </div>
    <div class="item-text" :style="activeTextStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  props: {
    path: String, //通过父组件动态传递路径过来
    activeTextColor: {
      //可以通过父组件传值 自定义每个item文字的颜色
      type: String,
      default: "hotpink",
    },
  },
  data() {
    return {
      // isActive: true,
    };
  },
  computed: {
    isActive() {
      return this.$route.path.includes(this.path); //判断当前处于活跃的路由路径 是否 包含传入的路径 如果包含item就显示红色 否侧显示黑色
    },
    activeTextStyle() {
      return this.isActive ? { color: this.activeTextColor } : {};
    },
  },
  methods: {
    itemClick() {
      // 解决多次点击同一个item控制台报错的bug
      // if (this.path == this.$route.path) {
      //   return;
      // }
      // 跳转相应的界面
      this.$router.replace(this.path).catch((err) => err);
    },
  },
};
</script>

<style scoped>
.tab-bar-item {
  flex: 1;
  text-align: center;
  height: 49px;
  font-size: 14px;
  cursor: pointer;
}
.tab-bar-item img {
  width: 24px;
  height: 24px;
  margin-top: 3px;
  vertical-align: middle;
}
</style>
